package com.baidu.infinity.ui.fragment.home.shape.shapes

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.os.Build
import androidx.annotation.RequiresApi
import com.baidu.infinity.ui.fragment.home.shape.BaseShape
import com.baidu.infinity.ui.fragment.home.shape.LayerManager
import com.baidu.infinity.ui.util.state.ShapeState
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin

class ArrowLineShape: BaseShape() {
    private var angle: Float = 0f
    private var length: Double = 0.0
    private var lineWidth = 0f
    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun draw(canvas: Canvas) {
        if(mState == ShapeState.SELECT){
            canvas.drawBitmap(mCornerBitmap,startX - mCornerSize / 2,startY - mCornerSize / 2,null)
            canvas.drawBitmap(mCornerBitmap,endX - mCornerSize / 2,endY - mCornerSize / 2,null)

        }
        mPaint.style = Paint.Style.STROKE
        mPath.reset()
        setArrowLineShape(mPath,startX,startY,endX,endY)
        canvas.drawPath(mPath,mPaint)
    }
    override fun updateSelectState(x: Float, y: Float) {
        mStartMoveX = x
        mStartMoveY = y
        if(x in startX - mCornerSize  ..startX + mCornerSize && y in startY - mCornerSize .. startY + mCornerSize ){
            mSelectedState = SelectState.LEFT
        }else if (x in endX - mCornerSize  ..endX + mCornerSize  && y in endY - mCornerSize.. endY + mCornerSize ){
            mSelectedState = SelectState.RIGHT
        }else{
            mSelectedState = SelectState.CENTER
        }
    }

    override fun setEndPoint(endX: Float, endY: Float) {
        super.setEndPoint(endX, endY)
        lineWidth = LayerManager.instance.mStrockSize
    }
    private fun setArrowLineShape(path:Path, startX: Float, startY: Float, endX: Float, endY: Float){
        length = Math.sqrt(((endY - startY) * (endY - startY) + (endX - startX) * (endX - startX)).toDouble()) / 6
        path.moveTo(startX, startY)
        path.lineTo(endX, endY)
        angle = atan2(startY - endY, startX - endX)
        path.lineTo((endX + length * cos(angle - Math.toRadians(30.0))).toFloat(), (endY + length * sin(angle - Math.toRadians(30.0))).toFloat())
        path.moveTo(endX, endY)
        path.lineTo((endX + length * cos(angle + Math.toRadians(30.0))).toFloat(), (endY + length * sin(angle + Math.toRadians(30.0))).toFloat())
    }
    override fun calculateLotation(x: Float, y: Float) {
        mMoveDX = x - mStartMoveX
        mMoveDY = y - mStartMoveY
        if(mSelectedState == SelectState.LEFT){
            startX = x
            startY = y
        }else if(mSelectedState == SelectState.RIGHT){
            endX = x
            endY = y
        }else if(mSelectedState == SelectState.CENTER){
            startX += mMoveDX
            endX += mMoveDX
            startY += mMoveDY
            endY += mMoveDY
        }
        mStartMoveX = x
        mStartMoveY = y
    }

    override fun containsPoint(x: Float, y: Float): Boolean {
        return distance(x,y,startX,startY) + distance(x,y,endX,endY) - distance(startX,startY,endX,endY) <= lineWidth
    }
    fun distance(x1: Float,y1: Float,x2: Float,y2: Float): Float {
        return Math.sqrt(((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)).toDouble()).toFloat()
    }
}